﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class RP02SUM : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI
        {
            get
            {
                return (INTEXDataSet)TryFindResource("iNTEXDataSet");
            }
        }
        public INTEXDataSet setWorker = new INTEXDataSet();
        RP02SUMData m_Data, m_DataX;
        RP02SUMReport m_rpt;
        public DataSet setResult;
        
        private DataRow dwUIABPOS;
        DataTable tablePBOrder;
        public RP02SUM()
        {
            InitializeComponent();
            this.Name = "织造机台统计";
            this.Title = "织造机台统计";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent(); 
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new RP02SUMData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            dateChooser = (DatePicker)this.FindName("DateChooser");
            dwUIABPOS = setUI.ABPOS.NewRow();
        }
        #endregion
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
           
                    if (setUI != null)
                    {
                        //setResult = m_Data.GetWASTAMM();
                        //setUI.Merge(setResult);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                zoneQueryCond.IsExpanded = false;
                setUI.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = txtLFD.Text.Trim();
                string POS = txtPOS.Text.Trim();

                //----------------
                KRS = "";
                LFD = "";
                POS = "";
                setUI.Tables["WBOrder"].Clear();
                //----------------
                dwUIABPOS["ABPERLKZ"] = " ";
                dwUIABPOS["ABPAUFKRS"] = KRS;
                dwUIABPOS["ABPAUFLFD"] = "";
                if (LFD.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFLFD"] = m_Data.Right("000000" + LFD, 6);
                }
                dwUIABPOS["ABPAUFPOS"] = "";
                if (POS.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFPOS"] = m_Data.Right("000" + POS, 3);
                }

                dwUIABPOS["ABPZUGET"] = "0";
                dwUIABPOS["ABPART"] = txtArticle.Text.ToString().Trim();
                string GISSUEStartDate = gsendstartdate.Text.ToString().Trim();
                string GISSUEEndDate = gsendenddate.Text.ToString().Trim();
                if (GISSUEStartDate == "")
                {
                    GISSUEStartDate = "20000101";
                }
                if (GISSUEEndDate == "")
                {
                    GISSUEEndDate = "29990101";
                }
                dwUIABPOS["GISSUEDATE"] = GISSUEStartDate;
                dwUIABPOS["FGETDATE"] = GISSUEEndDate;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    setResult = m_Data.GetABPOS(dwUIABPOS);
                    setUI.Merge(setResult);
                    totalrecord = setUI.ABPOS.Rows.Count;
                }
                foreach (DataRow dwNABPOS in setUI.ABPOS)
                {
                    m_Data = new RP02SUMData();
                    m_Data.tablePBOrder = setUI.Tables["WBOrder"];
                    m_Data.OracleDB = this.OracleDB;
                    m_Data.proxy = this.proxy;
                    m_Data.ThreadID = ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage(m_Data, dwNABPOS);
                }
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            { 
                tablePBOrder = new DataTable("WBOrder"); 
                tablePBOrder.Columns.Add("VKPAUFNUM", typeof(string));
                tablePBOrder.Columns.Add("KDKUNDNA", typeof(string));
                tablePBOrder.Columns.Add("VKPWUTERM", typeof(string));
                tablePBOrder.Columns.Add("ARTICLE", typeof(string));
                tablePBOrder.Columns.Add("STDBATCHNO", typeof(string));
                tablePBOrder.Columns.Add("SBEAMLENG", typeof(string));
                tablePBOrder.Columns.Add("SBEAMNO", typeof(string));
                tablePBOrder.Columns.Add("WEFTYARNSTATUS", typeof(string)); 
                tablePBOrder.Columns.Add("WBOrder", typeof(string));
                tablePBOrder.Columns.Add("WBFactoryName", typeof(string));
                tablePBOrder.Columns.Add("PBFactoryName", typeof(string));
                tablePBOrder.Columns.Add("PBRunning", typeof(System.Int16));
                tablePBOrder.Columns.Add("PBFinished", typeof(System.Int16));
                tablePBOrder.Columns.Add("PBWaiting", typeof(System.Int16));
                tablePBOrder.Columns.Add("PBPlan", typeof(System.Int16));
                setUI.Merge(tablePBOrder);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {

                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Date_GotFocus(object sender, RoutedEventArgs e)
        {

        }
        private void gsendstartDateChooser_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            gsendstartdate.Text = m_Data.GetDate(sender);
        }
        private void gsendendDateChooser_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            gsendenddate.Text = m_Data.GetDate(sender);
        } 
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        public void GenerateReport(string PrintType)
        {
            setUI.AcceptChanges();
            Process[] AllProcesses = Process.GetProcessesByName("EXCEL");

            // check to kill the right process
            /*foreach (Process ExcelProcess in AllProcesses)
            {
                ExcelProcess.Kill();
            } */
                m_DataX = new RP02SUMData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                m_rpt = new RP02SUMReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL"); 
        }

        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            //tb.Text = "";
        }
    }
}
